﻿package toolbar {
	import flash.display.Sprite;
	import toolbar.icon.Split;
	
	public class ButtonGroup extends Sprite {
		public var groupName:String;
		public var btnMap:Array;
		protected var btnList:Array;
		protected var split:Split; //分隔按钮组的小竖线
		protected var left:int; //从左至右每次添加按钮后记录的下次x坐标值
		
		public function ButtonGroup(name:String):void {
			groupName = name;
			btnMap = new Array();
			btnList = new Array();
			init();
		}
		
		private function init():void {
			//添加组开头的小竖线，并设置下次的x坐标值
			split = new Split();
			addChild(split);
			left = split.width;
		}
		
		protected function addButton(btn:IconButton):void {
			//防止名称冲突
			if (btnMap[btn.intro] == null) {
				btnMap[btn.intro] = btn;
				btnList.push(btn);
			}
			//设置位置
			btn.x = left;
			left += btn.width;
			addChild(btn);
		}
		
		public function showButton():void {
			for (var i:int = 0; i < btnList.length; i++) {
				btnList[i].show();
			}
		}
		public function hideButton():void {
			for (var i:int = 0; i < btnList.length; i++) {
				btnList[i].hide();
			}
		}
		
		public function activeButton(name:String):void {
			btnMap[name].active();
		}
		public function cancelButton(name:String):void {
			btnMap[name].cancel();
		}
		
		public function cancelAll(btn:SelectButton):void {
			//除点击按钮之外全部取消
			for each (var item:SelectButton in btnList) {
				if(item != btn && item != null) {
					item.cancel();
				}
			}
		}
		public function activeOne(name:String):void {
			var btn:SelectButton = btnMap[name];
			//激活唯一指定按钮，其余取消
			for each (var item:SelectButton in btnList) {
				if(item == btn) {
					item.active();
				}
				else {
					item.cancel();
				}
			}
		}
	}
}